import * as fsPromises from 'fs/promises'
import * as step from '@flow-step/step-toolkit'
import * as fs from 'fs'

export async function writeToFile(filePath: string, content: string): Promise<void> {
    await fsPromises.writeFile(filePath, content, 'utf-8');
}

export async function readFile(filePath: string): Promise<string> {
    try {
        const data = await fsPromises.readFile(filePath, 'utf-8')
        return data
    } catch (error:any) {
        step.error(`Error reading file ${filePath}` )
        throw error
    }
}

export async function checkCoverageFileExist(coverageFilePath: string): Promise<void> {
    try{
        await fsPromises.access(coverageFilePath, fs.constants.F_OK)
    }catch{
        throw new Error(`Report file ${coverageFilePath} does not exist, please rename your test report`)
    }
}